package com.xiao.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration // 声明一个 配置类  并自动配置
@EnableSwagger2 // 开启 Swagger  只要我们开启 Swagger 后有默认配置
public class SwaggerConfig implements WebMvcConfigurer {

    @Override
    // implements WebMvcConfigurer 和重写该方法是为了 解决 No mapping for GET /swagger-ui.html 问题
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        WebMvcConfigurer.super.addResourceHandlers(registry);
    }

    // ------------------ 覆盖主页信息 Start ------------------
    // 配置了 Swagger 中 Docket 的 bean 实例
    @Bean
    public Docket docket(){
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                . apis(RequestHandlerSelectors.basePackage("com.xiao.controller"))//  扫描指定包
             // .paths(PathSelectors.ant("/xiao/**"))// 指定请求内容 只扫描 /xiao 下面的所有请求 我们没有这个请求 所以一定是空的
                .build().groupName("项晓忠");


        return  docket;
    }

    // 配置 Swagger 信息 = apiInfo 这里就是 http://localhost:8080/swagger-ui.html 首页的内容 我们将覆盖它
    private ApiInfo apiInfo(){
        // 作者信息
        Contact contact = new Contact("项晓忠", "https://www.baidu.com/", "2991755428@qq.com");
        ApiInfo apiInfo =
                new  ApiInfo(
                        "博客 接口文档",
                        "这里写详细描述文档的内容",
                        "最新版本1.0",
                        "urn:tos",
                        contact,
                        "点击跳转百度",// 链接名字
                        "https://www.baidu.com/", // 链接
                        new ArrayList()
                );
        return apiInfo;
    }
    // ------------------ 覆盖主页信息 End ------------------
}